#! /bin/sh
# Copyright (c) 2001       SuSE GmbH Nuernberg, Germany.
#               2002, 2003 SuSE Linux AG, Nuernberg, Germany.
#
# Author: Wolfgang Rosenauer, 2000-2003
#
# /etc/init.d/radiusd
#
#   and symbolic its link
#
# /usr/bin/rcradiusd
#
### BEGIN INIT INFO
# Provides:          radiusd
# Required-Start:    $network $syslog $remotefs
# Should-Start:      $time ypbind smtp
# Required-Stop:     $syslog $remote_fs
# Should-Stop:       ypbind smtp
# Default-Start:     3 5
# Default-Stop:      0 1 2 6
# Short-Description: RADIUS-Server
# Description:       Remote Authentication Dialin User Server
### END INIT INFO

. /etc/init.d/functions

prog=radiusd

[ -e /etc/sysconfig/$prog ] && . /etc/sysconfig/$prog

exec=${exec:=/usr/sbin/$prog}
config_dir=${config_dir:=/etc/raddb}

test -x "$exec" || { echo "$exec not installed"; \
	if [ "$1" = "stop" ]; then exit 0;
	else exit 5; fi; }

configtest() {
    echo -n "Checking $prog configuration "
    out=`$exec -Cxl stdout -d $config_dir`; retval=$?
    out=`echo "${out}" | tail -n 1 | sed 's/^\s*ERROR:\s*\(.*\)\s*$/\1/'`

    [ $retval -ne 0 ] &&  echo "$out" 1>&2

    return $retval
}

rc_reset
case "$1" in
    start)
        configtest || { rc_failed 150; rc_exit; }
        echo -n "Starting RADIUS daemon "
        startproc $exec >/dev/null
        rc_status -v
        ;;

    stop)
        echo -n "Shutting down RADIUS daemon "
        killproc -TERM $exec
        rc_status -v
        ;;

    try-restart|condrestart)
        # If first returns OK call the second, if first or second command fails, set echo return value.
        if test "$1" = "condrestart"; then
            echo "${attn} Use try-restart ${done}(LSB)${attn} rather than condrestart ${warn}(RH)${norm}"
        fi
        $0 status
        if [ $? = 0 ] ; then
                $0 restart
        else
                rc_reset        # Not running is not a failure.
        fi
        rc_status
        ;;

    restart)
        # Stop the service and regardless of whether it was running or not, start it again.
        configtest || { rc_failed 150; rc_exit; }
        $0 stop
        $0 start
        rc_status
        ;;

    force-reload)
        # Signal the daemon to reload its config. Most daemons o this on signal 1 (SIGHUP).
        # If it does not support it, restart.
        configtest || { rc_failed 150; rc_exit; }
        echo -n "Reload RADIUS daemon "
        killproc -HUP $exec
        rc_status -v
        ;;

    reload)
        # Like force-reload, but if daemon does not support signalling, do nothing (!)
        configtest || { rc_failed 150; rc_exit; }
        echo -n "Reload RADIUS daemon "
        killproc -HUP $exec
        rc_status -v
        ;;

    status)
        echo -n "Checking for service radiusd "
        checkproc $exec
        rc_status -v
        ;;

    configtest|testconfig)
        configtest
        rc_status -v
        ;;

    debug)
        $0 status
        if [ $? -eq 0 ]; then
            echo -n "$prog already running; for live debugging see raddebug(8)"
            exit 151
        fi
        $exec -X -d "$config_dir" || exit $?
        exit 0
        ;;

    debug-threaded)
        $0 status
        if [ $? -eq 0 ]; then
            echo -n "$prog already running; for live debugging see raddebug(8)"
            exit 151
        fi
        $exec -f -xx -l stdout -d "$config_dir" || exit $?
        exit 0
        ;;

    *)
        echo "Usage: $0 {start|stop|status|try-restart|restart|force-reload|reload|configtest|debug|debug-threaded}"
        exit 1
        ;;
esac
rc_exit
